<?php
/**
 * axmethods.php file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */

session_start();
if(isset($_GET['a']))
{
    $id = $_GET['a'];
    if(isset($_SESSION[$id]))
    {
        $minfo = $_SESSION[$id];
        $ns = prepareMethods($minfo);
        $result = array();
        foreach($ns as $nsname => $methods)
        {
            $mlist = array();
            $nname = ucfirst(str_ireplace('controller','',$nsname));
            foreach($methods as $method)
            {
                $method['params'][]='action';
                $mparams = implode(',',$method['params']);
                $action = sprintf('var action = Ext.apply(action,{method:"post",params:{controller:"%s",method:"%s",mparams:_jstools.serialize([%s])},afxparams:{sender:this,shandler:action.returnHandler,mask:ext4yii.ajaxMethodMask(action)}, url: "%s", failure:ext4yii.defaultOnFailureHandler,success:ext4yii.ajaxMethodReturnHandler} ); ',
                    $method['controller'],
                    $method['method'],
                    $mparams,
                    $method['url']);

                $mlist[] = sprintf('%s: function(%s) { if(action==null) action={} ; if(!ext4yii.ajaxMethodMaskCheck("%s",action)) return;  %s ; ext4yii.performServerAction(action); } ',$method['method'],$mparams,$method['method'],$action);
            }
            $result[] = sprintf('%s: { %s }',$nname,implode(",\n",$mlist));
        }

        header('Content-type: application/javascript');
        echo sprintf('Ext.apply(ext4yii,{ AjaxMethods: { %s } });',implode(',',$result));
    }
}

function prepareMethods($minfo)
{
    $result = array();
    foreach($minfo as $method)
        if(!isset($result[$method['controller']]))
            $result[$method['controller']] = array();

    foreach($minfo as $method)
        $result[$method['controller']][] = $method;

    return $result;
}
?>
